/*
 * Copyright 2009-2010 Belmont Software Services
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.belmont.backup.gui;

import com.belmont.backup.Utils;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class ViewLogDialog extends JDialog implements ActionListener {
    JScrollPane sc;
    JTextArea text;

    public ViewLogDialog(Frame parent) {
	super(parent, "View backup client log", false);
	initComponents();
    }

    public void actionPerformed(ActionEvent l) {
    }

    void initComponents() {
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
	text = new JTextArea(30, 85);
	text.setEditable(false);
	sc = new JScrollPane(text);
	JButton refresh = new JButton("Refresh");
        refresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
		refresh();
            }
        });
	getContentPane().add(refresh, BorderLayout.SOUTH);
	getContentPane().add(sc, BorderLayout.CENTER);
	refresh();
	pack();
    }

    void refresh() {
	text.setText("");
	File lf = Utils.getLogFile();

	if (lf != null) {
	    try {
		DataInputStream din = new DataInputStream(new FileInputStream(lf));
		try {
		    String l;
		    while ((l = din.readLine()) != null) {
			text.append(l+"\n");
		    }
		} finally {
		    din.close();
		}
	    } catch (IOException ex) {
		ex.printStackTrace();
	    }
	}
    }
}